// pages/order/order.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    onid: 0,
    orderList: '',
    orderPage: '',
    arlteView: false,
    num: 0,
    nums: 0,
    statusd: 1,
    searchOne: '',
    searchTwo: ''
  },
  showView() {
    var _this = this
    _this.setData({
      arlteView: _this.data.arlteView ? false : true
    })
  },
  onLoad: function(options) {
    var _this = this,
      id = options.id == 0 ? 'today' : options.id
    _this.setData({
      onid: options.id
    })
    _this.orders(id)
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 1500)
  },
  onReady: function() {},
  onShow: function() {
    var _this = this
    _this.orders(_this.data.onid)
  },
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {
    var _this = this
    _this.showList(1)
  },
  // onShareAppMessage: function() {},
  //点击加载数据
  showList(e) {
    var _this = this,
      id = e,
      list = _this.data.orderList
    if (id == 0) {} else if (id == 1) {
      let orderPage = _this.data.orderPage
      if (!orderPage) {
        console.log('没有更多数据了')
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none'
        })
      } else {
        com.link(orderPage, {}, function(rep) {
          console.log(rep)
          for (let i in rep.data.data) {
            list.push(rep.data.data[i])
          }
          _this.setData({
            orderList: list,
            orderPage: rep.data.links.next
          })
        }, "GET")
      }
    }
    console.log(id);
  },
  liattap(e) {
    console.log(e)
    var _this = this,
      id = e.currentTarget.id
    _this.setData({
      onid: id
    })
    _this.orders(id == 0 ? 'today' : id)

  },
  codeList: function(e) {
    let index = e.currentTarget.dataset.index;
    let _this = this
    if (index == 0) {
      wx.navigateTo({
        url: '../orderdeliver/orderdeliver?id=' + e.currentTarget.id,
      })
    }
    if (index == 1) {
      wx.navigateTo({
        url: '/pages/logistics/logistics?id=' + e.currentTarget.id,
      })
    }
    if (index == 2) {
      wx.navigateTo({
        url: '/pages/returnGoods/returnGoods?id=' + e.currentTarget.id,
      })
    }
  },
  //获取商品列表
  orders(e) {
    console.log(e)
    var _this = this,
      url = app.link.orders,
      data = {
        page: 1,
        state: e
      }
    com.link(url, data, function(rep) {
      console.log(rep);
      _this.setData({
        orderList: rep.data.data,
        orderPage: rep.data.links.next
      })
    }, "GET")
  },

  jumplist(res) {
    wx.navigateTo({
      url: '../orderInfo/index?id=' + res.currentTarget.id,
    })
  },
  //回车键筛选
  searchConFirm(e) {
    console.log(e)
    var _this = this,
      value = e.detail.value
    com.link(app.link.orders, {
      search: value,
      state: _this.data.onid == 0 ? 'today' : _this.data.onid
    }, function(res) {
      console.log(res)
      var list = res.data.data
      // for (let i in res.data.data){
      //   list.push(res.data.data[i])
      // }
      _this.setData({
        orderList: list,
        orderPage: res.data.links.next,
        itemsValue: value
      })
    }, "GET")
  },
  sliderValue(e) {
    console.log(e)
    this.setData({
      num: e.detail.value
    })
  },
  sliderValues(e) {
    console.log(e)
    this.setData({
      nums: e.detail.value
    })
  },
  onBindInputSearch(e) {
    var _this = this,
      id = e.currentTarget.id,
      v = e.detail.value
    if (id == 1) {
      _this.setData({
        searchOne: v
      })
    } else {
      this.setData({
        searchTwo: v
      })
    }
  },
  //确定重置按钮
  searchOrder(e) {
    var _ = this,
      __ = e.currentTarget.id
    if (__ == 1) {
      _.setData({
        searchOne: '',
        searchTwo: ''
      })
    } else {
      wx.showToast({
        title: '加载中',
        icon: 'loading'
      })
      setTimeout(() => {
        com.link(app.link.orders, {
          state: _.data.onid,
          sn: _.data.searchTwo,
          consignee: _.data.searchOne
        }, function(res) {
          var list = res.data.data
          _.setData({
            orderList: list,
            orderPage: res.data.links.next,
            itemsValue: '',
            arlteView: false
          })
        }, 'GET')
      }, 1000)
    }
  }
})